package io.terminus.spzx.common.config.exception;

import com.google.common.base.Throwables;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 全局统一异常Handler处理类
 * @Date 2024-03-24
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理
     *
     * @param except 全局处理的异常
     * @return 处理以后的异常对象
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result<Void> globalExceptionHandler(Exception except) {
        log.error("globalExceptionHandler, except info={}", Throwables.getStackTraceAsString(except));

        return Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
    }

    /**
     * 自定义异常处理
     *
     * @param spzxExcept 自定义处理的异常
     * @return 处理以后的异常对象
     */
    @ResponseBody
    @ExceptionHandler(SpzxCustomException.class)
    public Result<Void> customExceptionHandler(SpzxCustomException spzxExcept) {
        log.error("customExceptionHandler, except info={}", Throwables.getStackTraceAsString(spzxExcept));

        return Result.build(null, spzxExcept.getResultCode());
    }
}
